﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace pyTivoMetadataManager
{
    public partial class SettingsForm : Form
    {
        MainForm _mainForm;
        private string _originalPath;

        public SettingsForm(MainForm mainForm)
        {
            InitializeComponent();
            _mainForm = mainForm;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_originalPath.Equals(txtMediaFolder.Text.Trim()))
            {
                SaveSettings();
                _mainForm.LoadMediaDirectory();
            }
            this.Close();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            SettingsSingleton settings = SettingsSingleton.Instance;
            txtMediaFolder.Text = _originalPath = settings.MediaPath;
        }

        private void SaveSettings()
        {
            SettingsSingleton settings = SettingsSingleton.Instance;
            settings.MediaPath = txtMediaFolder.Text;
            settings.Commit();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMediaFolder.Text.Trim()))
            {
                folderBrowserDialog1.SelectedPath = txtMediaFolder.Text.Trim();
            }
            DialogResult dr = folderBrowserDialog1.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    txtMediaFolder.Text = folderBrowserDialog1.SelectedPath;
                    break;
            }
        }
    }
}
